// 输入：nums1 = [1,2,2,1], nums2 = [2,2]
// 输出：[2]

function getCommon1 (nums1, nums2) {
    let numsShort;
    let numsLong;
    if (nums1.length < nums2.length) {
        numsShort = nums1;
        numsLong = nums2;
    } else {
        numsShort = nums2;
        numsLong = nums1;
    }

    const result = new Set();
    for (const num of numsShort) {
        if (numsLong.includes(num)) {
            result.add(num);
        }
    }
    return Array.from(result);
}

function getCommon2 (nums1, nums2) {
    const arr = nums1.filter((num) => {
        return nums2.includes(num);
    });
    return Array.from(new Set(arr));
}

// console.log(getCommon([1,2,2,1], [2,2])); // [2]
function makeData () {
    return {
        arr1: new Array(Math.floor(Math.random() * 1000)).fill(0).map(() => Math.floor(Math.random() * 1000)),
        arr2: new Array(Math.floor(Math.random() * 1000)).fill(0).map(() => Math.floor(Math.random() * 1000)),
    };
}

function performance (name, cb, count = 5000) {
    console.time(name);
    for (let i = 0; i < count; i++) {
        cb();
    }
    console.timeEnd(name);
}

performance('cuo', () => {
    const { arr1, arr2 } = makeData();
    getCommon1(arr1, arr2);
});

performance('geng cuo', () => {
    const { arr1, arr2 } = makeData();
    getCommon2(arr1, arr2);
});
